/*************************************************************************
 * $ Id : acd_svr.cpp    2011/08/25 10:36:00
 *
 *        swhors@naver.com
 *************************************************************************/

/*************************************************************************
 * history 
 * 2011/08/25 10:36:00  - initiated comment.
 *************************************************************************/

#include "config.h"

#include <iostream>
#include <vector>
#include <algorithm>

#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>

#include <signal.h>

using namespace std;

#include "constant.h"

#include "skylistener.h"
#include "ast_util.h"
#include "ast_db.h"
#include "ast_main.h"
#include "acd_manager.h"
#include "acd_svr.h"
#include "SkyConfUtil.h"

extern int gRunProcess;
extern int gConnected;

void *acdsvr_process( void * aArg )
{
   REQUESTSOCK * sSockInfo      = (REQUESTSOCK *)aArg;
   char          sFileName[64]  = {0,};
   char          sAddress[20]   = {0,};
   int           sCnt           = 0;
   int           sWriteLen      = 0;
   int           sReadLen       = 0;
   char          sReadBuff[512] = {0,};
   int           sLen           = 0;
   
   if( sSockInfo )
   {
      if( sSockInfo->mAcptSockfd >= 0 )
      {
         memset( sReadBuff, 0, 512 );
         sReadLen = read( sSockInfo->mAcptSockfd, sReadBuff, 512 );
         while( sReadLen > 0 )
         {
            inet_ntop( AF_INET,(void *)&sSockInfo->mCliAddr, sAddress, 20 );
            log_message( (char *)"INFO : recv msg from --> %s.\n", sAddress );
            log_message( (char *)"INFO : recv msg from --> %s.\n", sReadBuff ); 
            
            if( memcmp( sReadBuff, ACD_CMD_START, 5 ) == 0 )
            {
               run_acd_manager();
               sWriteLen = write( sSockInfo->mAcptSockfd, "200", 3 );
               if( sWriteLen > 0 )
               {
                  /* do nothing */
               }
               else
               {
               }
            } 
            else if( memcmp( sReadBuff, ACD_CMD_STOP, 4 ) == 0 )
            {
               stop_acd_manager();
               sWriteLen = write( sSockInfo->mAcptSockfd, "200", 3 );
               if( sWriteLen > 0 )
               {
                  /* do nothing */
               }
               else
               {
               }
            }
            else if( memcmp( sReadBuff, ACD_CMD_LOAD, 4 ) == 0 )
            {
               snprintf( sFileName, 64, "%s", &sReadBuff[5]);
#ifdef _DEBUG
               printf("[%s %d] %s\n", __FUNCTION__, __LINE__, sFileName );
#endif //_DEBUG

               sLen = strlen( sFileName );
               if( sLen > 0 )
               {
                  for( sCnt = 0; sCnt < sLen; sCnt ++ )
                  {
                     if( sFileName[sCnt ]=='\n' || sFileName[sCnt ]=='\r')
                        sFileName[sCnt ] = 0;
                  }
                  sLen = strlen( sFileName );
                  if( sLen > 0 )
                  {
#ifdef _DEBUG
                     printf("[%s %d] %s\n", __FUNCTION__, __LINE__, sFileName );
#endif //_DEBUG
                     add_acd_list( sFileName );
                  }
               }
               sWriteLen = write( sSockInfo->mAcptSockfd, "200", 3 );
               if( sWriteLen > 0 )
               {
                  /* do nothing */
               }
               else
               {
               }
            }
            else if( !memcmp( sReadBuff, ACD_CMD_RELOAD, 6 ) )
            {
               sWriteLen = write( sSockInfo->mAcptSockfd, "501", 3 );
               if( sWriteLen > 0 )
               {
                  /* do nothing */
               }
               else
               {
               }
            }
            else if( !memcmp( sReadBuff, ACD_CMD_KICKOUT, 7 ) )
            {
               kick_member( sReadBuff + 8 );
               sWriteLen = write( sSockInfo->mAcptSockfd, "200", 3 );
               if( sWriteLen > 0 )
               {
                  /* do nothing */
               }
               else
               {
               }
            }
            else if( !memcmp( sReadBuff, ACD_CMD_ADDMEMBER, 6 ) )
            {
               add_member( sReadBuff + 7 );
               sWriteLen = write( sSockInfo->mAcptSockfd, "200", 3 );
               if( sWriteLen > 0 )
               {
                  /* do nothing */
               }
               else
               {
               }
            }
            else if( !memcmp( sReadBuff, ACD_CMD_ADDCUSTOM, 6 ) )
            {
               add_customer( sReadBuff + 7 );
               sWriteLen = write( sSockInfo->mAcptSockfd, "200", 3 );
               if( sWriteLen > 0 )
               {
                  /* do nothing */
               }
               else
               {
               }
            }
            else if( !memcmp( sReadBuff, "exit",4 ) )
            {
                //exit( 0 );
               sWriteLen = write( sSockInfo->mAcptSockfd, "200", 3 );
               if( sWriteLen > 0 )
               {
                  /* do nothing */
               }
               else
               {
               }
               pid_t pid = getpid();
               kill( pid, SIGTERM ); 
            }
            else if( !memcmp( sReadBuff, "END", 3 ) )
            {
               sWriteLen = write( sSockInfo->mAcptSockfd, "200", 3 );
               if( sWriteLen > 0 )
               {
                  /* do nothing */
               }
               else
               {
               }
               break;
            }
            else
            {
               sWriteLen = write( sSockInfo->mAcptSockfd, "400", 3 );
               if( sWriteLen > 0 )
               {
                  /* do nothing */
               }
               else
               {
               }
               log_message( ( char *)"INFO : received illegal message from %s.\n",
                            sAddress ); 
            }
            memset( sReadBuff, 0, 512 );
            sReadLen = read( sSockInfo->mAcptSockfd, sReadBuff, 512 );
         }
         if( sSockInfo->mAcptSockfd > 0 )
         {
            close( sSockInfo->mAcptSockfd );
         }
         else
         {
            /* do nothing */
         }
      }
      else
      {
         /* do nothing */
      }
   }
   else
   {
      /* do nothing */
   }
   return NULL;
}

///////////////////////////////////////////////////////////////
